package com.ruoyi.web.controller.system;

import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.framework.web.service.TokenService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TourismActivities;
import com.ruoyi.system.service.ITourismActivitiesService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 特色活动Controller
 *
 * @author gqd
 * @date 2020-12-29
 */
@RestController
@RequestMapping("/system/activities")
public class TourismActivitiesController extends BaseController
{
    @Autowired
    private ITourismActivitiesService tourismActivitiesService;
    @Resource
    private TokenService tokenService;

    /**
     * 查询特色活动列表
     */
    @PreAuthorize("@ss.hasPermi('system:activities:list')")
    @GetMapping("/list")
    public TableDataInfo list(TourismActivities tourismActivities)
    {
        startPage();
        List<TourismActivities> list = tourismActivitiesService.selectTourismActivitiesList(tourismActivities);
        return getDataTable(list);
    }

    /**
     * 导出特色活动列表
     */
    @PreAuthorize("@ss.hasPermi('system:activities:export')")
    @Log(title = "特色活动", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TourismActivities tourismActivities)
    {
        List<TourismActivities> list = tourismActivitiesService.selectTourismActivitiesList(tourismActivities);
        ExcelUtil<TourismActivities> util = new ExcelUtil<TourismActivities>(TourismActivities.class);
        return util.exportExcel(list, "activities");
    }

    /**
     * 获取特色活动详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:activities:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tourismActivitiesService.selectTourismActivitiesById(id));
    }

    /**
     * 新增特色活动
     */
    @PreAuthorize("@ss.hasPermi('system:activities:add')")
    @Log(title = "特色活动", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TourismActivities tourismActivities)
    {

        System.out.println(tourismActivities.toString());
        return toAjax(tourismActivitiesService.insertTourismActivities(tourismActivities));
    }

    /**
     * 修改特色活动
     */
    @PreAuthorize("@ss.hasPermi('system:activities:edit')")
    @Log(title = "特色活动", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TourismActivities tourismActivities)
    {
        return toAjax(tourismActivitiesService.updateTourismActivities(tourismActivities));
    }

    /**
     * 删除特色活动
     */
    @PreAuthorize("@ss.hasPermi('system:activities:remove')")
    @Log(title = "特色活动", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tourismActivitiesService.deleteTourismActivitiesByIds(ids));
    }

    /**
     * 查询特色活动
     * @return
     */
    @PostMapping("/selectActivities")
    public AjaxResult selectActivities(){
        return tourismActivitiesService.selectActivities();
    }


    /**
     * 查询特色活动的详细信息
     * @param map
     * @return
     */
    @PostMapping("/selectActivitiesById")
    public AjaxResult selectActivitiesById(@RequestBody Map map, HttpServletRequest request){
        LoginUser loginUser = tokenService.getLoginUser(request);
        if(loginUser != null){
            Integer userId = loginUser.getUserId();
            Integer id = (Integer) map.get("id");
            return tourismActivitiesService.selectActivitiesById(id,userId);
        }else {
            Integer userId = null;
            Integer id = (Integer) map.get("id");
            return tourismActivitiesService.selectActivitiesById(id,userId);
        }
    }
}
